import { Control, ControlType, FontSizeType, TextAlignType } from './dashboard-model';
import { v4 } from 'uuid';
import { merge, map, omit } from 'ramda';

export const defaultControls = [{
  icon: 'title',
  type: ControlType.Header,
  label: '标题',
}, {
  icon: 'border_horizontal',
  type: ControlType.Hr,
  label: '分割',
}, {
  icon: 'text_format',
  type: ControlType.Text,
  label: '单行文本',
}, {
  icon: 'subject',
  type: ControlType.Textarea,
  label: '多行文本',
}, {
  icon: 'radio_button_checked',
  type: ControlType.Radio,
  label: '单选',
}, {
  icon: 'check_box',
  type: ControlType.Checkbox,
  label: '多选',
}, {
  icon: 'toggle_off',
  type: ControlType.Switch,
  label: '开关',
}, {
  icon: 'event_note',
  type: ControlType.Datetime,
  label: '日期时间',
}] as Control[];

export function configureControl(control: Control) {
  let config: any = {
    name: null,
    uuid: v4(),
    minItemCols: 3,
    maxItemCols: 12,
    cols: 0,
    rows: 0,
    x: 0,
    y:0,
  };

  switch (control.type) {
    case ControlType.Header:
      Object.assign(config, {
        maxItemRows: 1,
        minItemRows: 1,
        fontSize: FontSizeType.Large,
        textAlign: TextAlignType.Left,
      });
      break;
    case ControlType.Hr:
      Object.assign(config, {
        maxItemRows: 1,
        minItemRows: 1,
      });
      break;
    case ControlType.Text:
      Object.assign(config, {
        minItemRows: 2,
        maxItemRows: 2,
        placeholder: '请填写',
        isRequired: false,
      });
      break;
    case ControlType.Textarea:
      Object.assign(config, {
        minItemRows: 3,
        placeholder: '请填写',
        isRequired: false,
      });
      break;
    case ControlType.Radio:
      Object.assign(config, {
        minItemRows: 2,
        maxItemRows: 2,
        placeholder: '请填写',
        options: JSON.stringify([{
          key: v4(),
          value: '选项一',
          checked: true,
        }, {
          key: v4(),
          value: '选项二',
          checked: false,
        }, {
          key: v4(),
          value: '选项三',
          checked: false,
        }]),
      });
      break;
    case ControlType.Checkbox:
      Object.assign(config, {
        minItemRows: 3,
      });
      break;
    case ControlType.Switch:
      Object.assign(config, {
        minItemRows: 2,
        maxItemRows: 2,
      });
      break;
    case ControlType.Datetime:
      Object.assign(config, {
        minItemRows: 2,
        maxItemRows: 2,
      });
      break;
    default:
      break;
  }

  return merge(config, control);
}

export function configureControls(controls: Control[]) {
  return map(control => {
    return configureControl(control);
  }, controls)
}

export function filterUnnecessaryConfig(controls: Control[]) {
  return map(control => {
    return omit([
      'maxItemCols',
      'maxItemRows',
      'minItemCols',
      'minItemRows',
      'uuid',
    ], control) as Control;

  }, controls);
}


